/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QApplication>

#include "Options.h"

Options::Options():
	isPaused(false),
	language("English"),
	useSystemDate(true),
	currentDate(QDate::currentDate()),
	lastAccount(""),
	hasLogined(false),
	autoSpeak(false),
	quickSelectWordCount(50),
	selectWordPolicy(0),
	scanRangePolicy(0),
	testWordCountMin(15),
	firstGroupWaitSecMax(10),
	secondGroupWaitSecMin(2),
	mainWindowStatus(Qt::WindowMaximized),
	autoStartTrainer(false),
	trainEpochMS(2000),
	checkFinishedEpochCount(30),
	finishedMSEOfMSEs(0.0001f),
	trainEpochCountMax(1000)
{
	QPixmap pixmap(":/pixmaps/gre.png");
	splash.setPixmap(pixmap);
	readSettings();
}

Options::~Options()
{
	writeSettings();
}

void Options::readSettings()
{
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );
	QVariant val;

	settings.beginGroup( "MainWindow" );

	val = settings.value("Geometry");
	if(val.isValid()) mainWinGeo = val.toRect();

	val = settings.value("MainWindowStatus");
	if(val.isValid()) mainWindowStatus = (Qt::WindowState)val.toInt();

	settings.endGroup();

	settings.beginGroup( "Global" );

	val = settings.value("Language");
	if(val.isValid()) language = val.toString();

	val = settings.value("LastAccount");
	if(val.isValid()) lastAccount = val.toString();

	val = settings.value("ApplicationFont");
	if(val.isValid()) programFont.fromString(val.toString());

	val = settings.value("BrowserFont");
	if(val.isValid()) wordBrowserFont.fromString(val.toString());

	val = settings.value("AutoSpeak");
	if(val.isValid()) autoSpeak = val.toBool();

	val = settings.value("UseSystemDate");
	if(val.isValid()) useSystemDate = val.toBool();

	val = settings.value("CurrentDate");
	if(val.isValid()) currentDate = QDate::fromString(val.toString(), DATE_FORMAT);

	settings.endGroup();

	settings.beginGroup( "TestAnalysis" );

	val = settings.value("selectWordPolicy");
	if(val.isValid()) selectWordPolicy = val.toInt();

	val = settings.value("scanRangePolicy");
	if(val.isValid()) scanRangePolicy = val.toInt();

	val = settings.value("QuickSelectWordCount");
	if(val.isValid()) quickSelectWordCount = val.toInt();

	settings.endGroup();

	settings.beginGroup( "Test" );

	val = settings.value("TestWordCountMin");
	if(val.isValid()) testWordCountMin = val.toInt();

	val = settings.value("FirstGroupWaitSecMax");
	if(val.isValid()) firstGroupWaitSecMax = val.toInt();

	val = settings.value("SecondGroupWaitSecMin");
	if(val.isValid()) secondGroupWaitSecMin = val.toInt();

	settings.endGroup();

	settings.beginGroup( "ANN" );

	val = settings.value("AutoStartTrainer");
	if(val.isValid()) autoStartTrainer = val.toBool();

	val = settings.value("TrainIntervalMS");
	if(val.isValid()) trainEpochMS = val.toInt();

	val = settings.value("CheckFinishedEpochCount");
	if(val.isValid()) checkFinishedEpochCount = val.toInt();

	val = settings.value("FinishedMSEOfMSEs");
	if(val.isValid()) finishedMSEOfMSEs = val.toDouble();

	val = settings.value("TrainEpochCountMax");
	if(val.isValid()) trainEpochCountMax = val.toInt();

	settings.endGroup();
}

void Options::writeSettings()
{
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );

	settings.beginGroup( "MainWindow" );
	settings.setValue("Geometry", mainWinGeo);
	settings.setValue("MainWindowStatus", (int)mainWindowStatus);
	settings.endGroup();

	settings.beginGroup( "Global" );
	settings.setValue("Language", language);
	settings.setValue("LastAccount", QString("%1").arg(lastAccount));
	settings.setValue("ApplicationFont", programFont.toString());
	settings.setValue("BrowserFont", wordBrowserFont.toString());
	settings.setValue("AutoSpeak", autoSpeak);
	settings.setValue("UseSystemDate", useSystemDate);
	settings.setValue("CurrentDate", currentDate.toString(DATE_FORMAT));
	settings.endGroup();

	settings.beginGroup("TestAnalysis");
	settings.setValue("selectWordPolicy", QString("%1").arg(selectWordPolicy));
	settings.setValue("scanRangePolicy", QString("%1").arg(scanRangePolicy));
	settings.setValue("QuickSelectWordCount", QString("%1").arg(quickSelectWordCount));
	settings.endGroup();

	settings.beginGroup( "Test" );
	settings.setValue("TestWordCountMin", testWordCountMin);
	settings.setValue("FirstGroupWaitSecMax", firstGroupWaitSecMax);
	settings.setValue("SecondGroupWaitSecMin", secondGroupWaitSecMin);
	settings.endGroup();

	settings.beginGroup( "ANN" );
	settings.setValue("AutoStartTrainer", autoStartTrainer);
	settings.setValue("TrainIntervalMS", trainEpochMS);
	settings.setValue("CheckFinishedEpochCount", checkFinishedEpochCount);
	settings.setValue("FinishedMSEOfMSEs", finishedMSEOfMSEs);
	settings.setValue("TrainEpochCountMax", trainEpochCountMax);
	settings.endGroup();
}

QRect Options::scaleGeometry(const QRect& parent, float widthRate, float heightRate)
{
	QRect r;
	float parentX = parent.x();
	float parentY = parent.y();
	float parentWidth = parent.width();
	float parentHeight = parent.height();
	r.setX(parentX + (1.0f - widthRate)*parentWidth/2);
	r.setY(parentY + (1.0f - heightRate)*parentHeight/2);
	r.setWidth(parentWidth*widthRate);
	r.setHeight(parentHeight*heightRate);
	return r;
}

void Options::setCursorBusy(bool busy)
{
	if (busy)
		QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
	else
		QApplication::restoreOverrideCursor();
}

QDate Options::getCurrendDate()
{
	if (useSystemDate)
		return QDate::currentDate();
	else
		return currentDate;
}

